package com.base.lock.redis;

import java.util.Arrays;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.connection.RedisStringCommands.SetOption;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.base.lock.redis.example.RedisLock;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/redis")
public class RedisController {

	@Autowired
	private RedisTemplate redisTemplate;

	@GetMapping("/lock")
	public String lock() {
		log.info("我进入了方法!");
		try (RedisLock lock = new RedisLock(redisTemplate, "redisKey", 30)) {
			if (lock.getLock()) {
				log.info("我获取到了锁！");
				Thread.sleep(5000);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		log.info("方法执行完成");
		return "方法执行完成";
	}

}
